package com.project.business.component.tool;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import com.project.model.jdbc.DataSourceSwitcher;

/**
 * 
 * @author xiaoxiaosu
 * 
 */
public class DBOperate {
	private JdbcTemplate jdbcTemplate;
	// setMaxRow
	//private final int MAX_ROW = 1000000;
	// setQueryTimeOut
	//private final int TIME_OUT = 600000;

	/***************** Update and Insert **********************/
	public void execute(String sql, Object[] params) throws Exception {
		execute(sql, params, false);
	}

	/**
	 * 
	 * @param sql
	 * @param obj
	 * 
	 * @param isAutoIncr
	 * @return
	 */
	public long execute(String sql, Object[] params, boolean isAutoIncr)
			throws Exception {
		long result = 0;
		DataSourceSwitcher.setMaster();
		jdbcTemplate.update(sql, params);
		if (isAutoIncr == true && isInsert(sql)) {
			sql = "select @@identity as id";
			result = jdbcTemplate.queryForLong(sql);
		}
		return result;
	}

	public void execute(String sql) throws Exception {
		execute(sql, false);
	}

	/**
	 * 
	 * @param sql
	 * @param isAutoIncr
	 * @return
	 */
	public long execute(String sql, boolean isAutoIncr) throws Exception {
		long result = 0;
		DataSourceSwitcher.setMaster();
		jdbcTemplate.update(sql);
		if (isAutoIncr == true && isInsert(sql)) {
			sql = "select @@identity as id";
			result = jdbcTemplate.queryForLong(sql);
		}
		return result;
	}

	/**
	 * 
	 * @param sql
	 */
	public boolean createTable(String sql) throws Exception {
		DataSourceSwitcher.setMaster();
		if (isCreate(sql)) {
			jdbcTemplate.execute(sql);
			return true;
		}
		return false;
	}

	/*************************** select ***************************/
	public long queryForLong(String sql, Object[] params) throws Exception {
		DataSourceSwitcher.setSlave();
		return jdbcTemplate.queryForLong(sql, params);
	}

	public long queryForLong(String sql) {
		DataSourceSwitcher.setSlave();
		return jdbcTemplate.queryForLong(sql);
	}

	public int queryForInt(String sql, Object[] params) throws Exception {
		DataSourceSwitcher.setSlave();
		return jdbcTemplate.queryForInt(sql, params);
	}

	public int queryForInt(String sql) throws Exception {
		DataSourceSwitcher.setSlave();
		return jdbcTemplate.queryForInt(sql);
	}

	public SqlRowSet queryForRowSet(String sql, Object[] params)
			throws Exception {
		DataSourceSwitcher.setSlave();
		return jdbcTemplate.queryForRowSet(sql, params);
	}

	public SqlRowSet queryForRowSet(String sql) throws Exception {
		DataSourceSwitcher.setSlave();
		return jdbcTemplate.queryForRowSet(sql);
	}

	public List<Map<String, Object>> queryForList(String sql) throws Exception {
		DataSourceSwitcher.setSlave();
		return jdbcTemplate.queryForList(sql);
	}

	public List<Map<String, Object>> queryForList(String sql, Object[] params)
			throws Exception {
		DataSourceSwitcher.setSlave();
		return jdbcTemplate.queryForList(sql, params);
	}

	// public List queryForBean(String sql, Class<?> clazz) {
	// DataSourceSwitcher.setSlave();
	// return jdbcTemplate.queryForObjects(sql, requiredType)
	// }

	/**
	 * 这个查询只能是查询一条记录的查询,返回一个map,key的值是column的值
	 * 
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> queryForMap(String sql) throws Exception {
		DataSourceSwitcher.setSlave();
		return jdbcTemplate.queryForMap(sql);
	}

	public <T> List<T> query(String sql, RowMapper<T> rowMapper)
			throws Exception {
		DataSourceSwitcher.setSlave();
		return jdbcTemplate.query(sql, rowMapper);
	}

	public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object[] objs)
			throws Exception {
		DataSourceSwitcher.setSlave();
		return jdbcTemplate.query(sql, rowMapper, objs);
	}

	public void query(String sql, RowCallbackHandler rcm) throws Exception {
		DataSourceSwitcher.setSlave();
		jdbcTemplate.query(sql, rcm);
	}

	public void query(String sql, RowCallbackHandler rcm, Object[] objs)
			throws Exception {
		DataSourceSwitcher.setSlave();
		jdbcTemplate.query(sql, rcm, objs);
	}

	/**
	 * 这个查询只能是查询一条记录的查询,返回一个map,key的值是column的值
	 * 
	 * @param sql
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> queryForMap(String sql, Object[] params)
			throws Exception {
		DataSourceSwitcher.setSlave();
		return jdbcTemplate.queryForMap(sql, params);
	}

	/**
	 * 查询单个
	 * 
	 * @param <T>
	 * @param sql
	 * @param elementType
	 * @return
	 */
	public <T> List<T> queryForList(String sql, Class<T> elementType) {
		DataSourceSwitcher.setSlave();
		return jdbcTemplate.queryForList(sql, elementType);
	}

	// public <T>List<T> queryList(String sql,Class clazz){
	// return jdbcTemplate.query(sql, new BeanPropertyRowMapper(clazz));
	// }
	/****************************************** assist function *********************************************************/
	/**
	 * check insert sql
	 * 
	 * @param sql
	 * @return
	 */
	private boolean isInsert(String sql) {
		if (sql.trim().substring(0, 6).equalsIgnoreCase("insert"))
			return true;
		return false;
	}

	private boolean isCreate(String sql) {
		if (sql.trim().substring(0, 6).equalsIgnoreCase("create"))
			return true;
		return false;
	}

	/**** SET && GET ***/
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
}
